<?php
/**
 * Coffee Framework

 * @category   CF
 * @package    CF_db
 * @author     그네 <Jung Sik, Park> <ccooffeee at hotmail dot com>
 * @copyright  Copyright (c) 2009 Jung Sik, Park <ccooffeee at hotmail dot com>
 * @license    doc/LICENSE    Based New BSD License
 * @version    $Id: db.php,v 0.1.3 2010/08/09 ccooffeee Exp $

 * LICENSE

 * 본 프로그램은 New BSD License 를 기본으로 하고 있지만 약간 다른 제약조건을 가지고 있습니다. 
 * 함께 제공된 license file 인  doc/LICENSE 를 꼭 확인하시길 바랍니다.

 * This Source is subject to the New BSD License with SOME CONSTRAINT that is boudled
 * with this package in the file doc/LICENSE 
 */


require_once __DIR__.'/abstract.php';


class CF_db_sqlsrv extends CF_db_abstract
{
// {{{ properties
	private $savepoint = null;
// }}}

// {{{ void begin ()
	public function begin() {
		$this->connect();
		if($this->transaction === true) return;

		$this->exec("BEGIN TRANSACTION");
		$this->transaction = true;
	}
// }}}
// {{{ void commit ()
	public function commit () {
		if($this->transaction !== true) return;

		$this->exec("COMMIT TRANSACTION");

		$this->savepoint = null;
		$this->transaction = false;
	}
// }}}
// {{{ void rollback ([string savepoint])
	public function rollback ($savepoint = null) {
		if($this->transaction !== true) return;

		$this->exec("ROLLBACK TRANSACTION");
		$this->savepoint = null;
		$this->transaction = false;
	}
// }}}
// {{{ final void setSavepoint (string savepoint)
    final public function setSavepoint($pt = null) {
		if($this->dao === null) return;
		if($this->transaction !== true) return;

		$sp = $this->getSavepoint();
		if($sp) $this->exec("RELEASE SAVEPOINT ".$sp);
		if($pt) $this->exec('SAVEPOINT '.$pt);
		$this->savepoint = $pt;
	}
// }}}

// {{{ string insertId ([string seq_name])
	public function insertId ($dummy = null) {
		$query = 'SELECT @@IDENTITY';
		return $this->query($query)->field();
	}
// }}}

// {{{ string escape (string)
	public function escape ($str) {
		if($this->_isQuoted())
			$str = stripslashes($str);

		return str_replace("'", "''", $str);

    }
// }}}


// {{{ protected methods
	// {{{ string configToDsn ()
	protected function configToDsn() {
		if($this->config === null) return null;

		if(!isset($this->config['driver']) || !$this->config['driver'])
			return null;
/*
		switch($this->config['driver']) {
			case 'freetds':
			case 'sybase':	$drv = 'sybase'; break;
			case 'mssql':	$drv = 'mssql';  break;
			case 'dblib':
			default:
				$drv = 'dblib';  break;
		}
*/
		$drv = "sqlsrv";
		
		$dsn = $drv.':';
		if(!isset($this->config['server']) || !$this->config['server'])
			return null;

		$host = $this->config['server'];
		if(isset($this->config['port']) && $this->config['port']) {
			$seperator = ':';
			if(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN')
				$seperator = ',';
			$host .= $seperator.$this->config['port'];
		}

		$dsn .= 'server='.$host.';';
		$dsn .= 'Database='.$this->config['name'];

		return $dsn;
	}
	// }}}
	// {{{ void init ()
	protected function init() {
		parent::init();

		$this->exec('SET QUOTED_IDENTIFIER ON');
	}
	// }}}
// }}}

// {{{ private string getSavepoint ()
	private function getSavepoint () {
		return $this->savepoint;
	}
// }}}

}

// vim: ts=4 sw=4 fdm=marker
?>
